package cn.tedu.sp04.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2022/1/14 16:42
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private ItemClient itemClient;

    @Override
    public Order getOrder(String orderId) {
        log.info("获取订单:{}",orderId);
        final Order order = new Order();
        order.setId(orderId);
        log.info("订单：{}",order);
        JsonResult<List<Item>> remoteItem = itemClient.getOrder(orderId);
        // TODO: 远程调用商品，获取商品列表
        order.setItems(remoteItem.getData());

        // TODO: 远程调用用户，获取用户
        JsonResult<User> remoteUser = userClient.getUser(7);
        log.info("data的类型:{}\ndata:{}",remoteUser.getData().getClass().getName(),remoteUser.getData());

        order.setUser(remoteUser.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("新增订单:{}", order);
        System.out.println(itemClient.decreaseNumber(order.getItems()));
        System.out.println(userClient.addUserScore(order.getUser().getId(), 1000));
    }
}
